package csv;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.e885.log.E885Logger;
import com.e885.util.string.StringUtil;

/**
 * url: http://127.0.0.1:8080/ExcelExport/csv/csvExport<br>
 * <br>
 * 輸出報表 excel, 但是用 .csv 的格式<br>
 * 
 * @author Grady.Kao
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2000-2008
 */

public class CSVExportServlet extends HttpServlet
{
	private static final long serialVersionUID = 6907134855357039412L;
	
	private final String DELIM = ",";
	private final String QUOTE = "\"";

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		String[] headers = { "姓名", "密碼" };
		String[] contents = { "Grady.Kao", "1234" };
		
		E885Logger.servletLog.debug( "headers:" + headers );
		
		//輸出儲檔時, 使用預期的 report.csv
		response.setHeader( "Content-Disposition", "inline;filename=report.csv" );
		
		//content type 使用  excel 的格式, PS: charset 一定要使用 Big5(目前不知是什麼原因)
		response.setContentType( "application/vnd.ms-excel; charset=Big5" );
		
		PrintWriter writer = response.getWriter();		
		writer.println( StringUtil.composeByArray( headers, DELIM, QUOTE ) );
		writer.println( StringUtil.composeByArray( contents, DELIM, QUOTE ) );
		
		
		writer.close();
	}
}
